package com.furongji.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.furongji.common.domain.query.TeacherQuery;
import com.furongji.education.domain.po.Teacher;
import com.furongji.education.domain.po.Course;
import com.furongji.education.mapper.TeacherMapper;
import com.furongji.education.service.CourseService;
import com.furongji.education.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author furongji
 * @since 2025-05-16
 */
@Service
@RequiredArgsConstructor
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    private final TeacherMapper mapper;

    private final CourseService courseService;

    private final RedisTemplate<String, Object> redisTemplate;

    @Override
    public IPage<Teacher> pageQuery(IPage<Teacher> page, TeacherQuery teacherQuery) {
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Teacher::getGmtCreate);
        if (teacherQuery == null) {
            return mapper.selectPage(page, wrapper);
        }
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like(Teacher::getName, name);
        }
        if (level != null && level != 0) {
            wrapper.eq(Teacher::getLevel, level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge(Teacher::getGmtCreate, begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le(Teacher::getGmtCreate, end);
        }
        System.out.println("service_total:" + mapper.selectPage(page, wrapper).getTotal());
        System.out.println("service_rows:" + mapper.selectPage(page, wrapper).getRecords());
        return mapper.selectPage(page, wrapper);
    }

    @Override
    public Map<String, Object> getApiTeacherInfo(String teacherId) {
        Teacher teacher = super.getById(teacherId);
        List<Course> courseList = courseService.getApiCourseInfoByTeacherId(teacherId);
        Map<String, Object> mapResult = new HashMap<>();
        mapResult.put("teacher", teacher);
        mapResult.put("courseList", courseList);
        return mapResult;
    }
}
